#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    vector<int> inventoryManagement(vector<int>& stock, int cnt) {
        srand(time(0));
        qsort(stock, 0, stock.size() - 1, cnt);
        return { stock.begin(),stock.begin() + cnt };
    }
    void qsort(vector<int>& stock, int l, int r, int k)
    {
        if (l >= r) return;
        int key = getRandom(stock, l, r);
        int i = l, left = l - 1, right = r + 1;
        while (i < right)
        {
            if (stock[i] < key) swap(stock[i++], stock[++left]);
            else if (stock[i] == key) i++;
            else swap(stock[i], stock[--right]);
        }
        int a = left - l + 1, b = right - left - 1;
        if (a > k) qsort(stock, l, left, k);
        else if (a + b >= k) return;
        else qsort(stock, right, r, k - a - b);
    }
    int getRandom(vector<int>stock, int left, int right)
    {
        return stock[rand() % (right - left + 1) + left];
    }
};